<?php

namespace app\index\controller;
use app\index\model\Article;
use app\index\model\Collect;
use app\index\model\Comment;
use app\index\model\Group;
use app\index\model\User;
use think\Controller;
header('Access-Control-Allow-Origin:*');//允许所有来源访问
header('Access-Control-Allow-Method:POST,GET');//允许访问的方式
class Page extends  Controller {
    /**
     * @return json 登陆结果
     */
    public function login() {
        $data = input('post.');

            $user = new User();
            $res = $user->loginCheck($data);
            if($res) {
                $arr = array(
                    'code'=>1,
                    'msg'=>'登陆成功',
                    'data'=>$res
                );
                return json_encode($arr);
            }else {
                $arr = [
                    'code'=>0,
                    'msg'=>'用户名或密码错误'
                ];
                return json_encode($arr);
            }


    }

    /**
     * @return 注册
     */
    public function register() {
        $data = input('post.');
        $user = new User();
        $res = $user->register($data);
        if($res) {
            $arr = array(
                'code'=>1,
                'msg'=>'注册成功',
            );
            return json_encode($arr);
        }else {
            $arr = [
                'code'=>0,
                'msg'=>'注册失败'
            ];
            return json_encode($arr);
        }
    }

    /**
     * @return string 分类信息
     */
    public function selGroup() {
        $group = new Group();
        $res = $group->apiSel();
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查询分类信息成功',
                'data'=>$res
            );
            return json_encode($data);
        }else {
            $data = array(
                'code'=>0,
                'msg'=>查询分类信息失败,

            );
            return json_encode($data);
        }
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 排行榜
     */
    public function rank() {
        $group = new Article();
        $res = $group->downApi();

        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查询排行成功',
                'data'=>$res
            );
            return json_encode($data);
        }else {
            $data = array(
                'code'=>0,
                'msg'=>'查询排行失败',
                'data'=>''
            );
            return json_encode($data);
        }
    }

    /**
     * @return string文章内容
     */
    //获取所有文章内容
    public function contentAll() {
        $art = new Article();
        $res = $art->selAllApi();
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查询成功',
                'data'=>$res
            );
            return json_encode($data);
        }
    }
    //查询指定的文章
    public function article() {
        $id= input('post.id');
        $art = new Article();
        $res = $art->selOneApi($id);
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查询成功',
                'data'=>$res
            );
            return json_encode($data);
        }

    }
    //获取指定分类的内容
    public function content() {
        $class_id = input('post.id');
        $art = new Article();
        $res = $art->selApi($class_id);
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查询文章成功',
                'data'=>$res
            );
            return json_encode($data);
        }else {
            $data = array(
                'code'=>0,
                'msg'=>'查询文章失败',

            );
            return json_encode($data);
        }

    }

    /**
     * 点赞
     */
    public function zan() {
        $id = input('post.id');
        $art = new Article();
        $res = $art->where('id',$id)->setInc('zan',1);
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'点赞成功，你还可以继续点赞哦！'
            );
            return json_encode($data);
        }
    }

    /*
     * 搜索
     */
    public function sousuo() {
        $str = input('post.title');
//        echo $str;
//        exit;
        //模糊查询
        $art = new Article();
        $res = $art->where('title','like',"%".$str."%")->select();
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'搜索成功！',
                'data'=>$res
            );
            return json_encode($data);

        }else {
            $data = array(
                'code'=>0,
                'msg'=>'搜索失败',


            );
            return json_encode($data);
        }
    }
    /*
     * 收藏
     */
    public function collect() {
        $article_id = input('post.id');
        $user_id = input('post.user_id');
        $col = new Collect();
        $col->article_id = $article_id;
        $col->user_id = $user_id;
        if($res = $col->save()) {
            $data = array(
                'code'=>1,
                'msg'=>'收藏成功！'
            );
            return json_encode($data);

        }else {
            $data = array(
                'code'=>0,
                'msg'=>'收藏失败',
            );
            return json_encode($data);
        }
    }
    //查看收藏
    public function selCollection() {
        $user_id = input('post.id');
//        echo $user_id;
//        exit;
        $col = new Collect();
        $res = $col->sel($user_id);
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查看收藏成功！',
                'data'=>collection($res)->toArray()
            );
            return json_encode($data);

        }else {
            $data = array(
                'code'=>0,
                'msg'=>'您还没有收藏',
            );
            return json_encode($data);
        }
    }

    /*
     * 增加评论
     */
    public function comment() {
        $data = input('post.');
        $res = Comment::create($data);
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'评论成功'
            );
            return json_encode($data);
        }else {
            $data = array(
                'code'=>0,
                'msg'=>'评论失败'
            );
            return json_encode($data);
        }
    }
    //查询评论内容
    public function selComment() {
        $article_id = input('post.id');
        $com = new Comment();
        $res = $com->where('article_id',$article_id)->join('user','user_id=user.id')->field('user.username,data,news_comment.create_time')->select();
//        dump($res);
//        exit;
        if($res) {
            $data = array(
                'code'=>1,
                'msg'=>'查询评论成功',
                'data'=>collection($res)->toArray()
            );
            return json_encode($data);
        }else {
            $data = array(
                'code'=>0,
                'msg'=>'获取评论失败'
            );
            return json_encode($data);
        }
    }
}